package patrol.mobile.com.mobilepatrol.adapter;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.util.List;

import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.bean.RouteSurveyTaskStructBO;
import patrol.mobile.com.mobilepatrol.bean.TrafficInvestBaseInfo;

/**
 * Created by 健 on 2014/10/17.
 */
public class InvestWebContentAdapter extends PagerAdapter {
    private RouteSurveyTaskStructBO structInfo;
    private TrafficInvestBaseInfo investBaseInfo;
    private String dataStr;
    private List<View> contentView;

    public InvestWebContentAdapter(RouteSurveyTaskStructBO structInfo, List<View> contentView, String dataStr, TrafficInvestBaseInfo investBaseInfo) {
        this.structInfo=structInfo;
        this.contentView = contentView;
        this.dataStr=dataStr;
        this.investBaseInfo=investBaseInfo;
    }

    @Override
    public int getCount() {
        return contentView.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        WebView wb= (WebView) contentView.get(position);
        wb.loadUrl(wb.getTag().toString());
        wb.setWebViewClient(webViewClient);

        container.addView(wb);

        return wb;
    }

    private WebViewClient webViewClient=new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:updateTable("+dataStr+")");

            int position=(Integer)view.getTag(R.id.web_index);

      //      int km= getKm(structInfo.getStartPotName());
            int km= structInfo.getPotKm();
            if(investBaseInfo.getStartPotMeter()>0&&km==investBaseInfo.getStartPotKm()){
                int startIndex=investBaseInfo.getStartPotMeter()/100;
                if(position==0){
                    if(startIndex<5&&startIndex>0) {
                        view.loadUrl("javascript:initStartTab(" + startIndex + ")");
                    }else{
                        view.loadUrl("javascript:initStartTab(5)");
                    }
                }else if(position==1&&startIndex>=5){
                    view.loadUrl("javascript:initStartTab("+(startIndex-5)+")");
                }
            }
            if(investBaseInfo.getEndPotMeter2()>0&&km==investBaseInfo.getEndPotKm2()){
                int endIndex=investBaseInfo.getEndPotMeter2()/100;
                if(position==0&&endIndex<5&&endIndex>0){
                    view.loadUrl("javascript:initEndTab("+endIndex+")");
                }else if(position==1){
                    if(endIndex>=5) {
                        view.loadUrl("javascript:initEndTab(" + (endIndex - 5) + ")");
                    }else {
                        view.loadUrl("javascript:initEndTab(-1)");
                    }
                }
            }
            view.loadUrl("javascript:showFirstPage()");

        }
    };

    private int getKm(String str){
        int result=0;
        int i=str.indexOf("+");

        if(i>0){
            result=Integer.valueOf(str.substring(1,i));
        }

        return result;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view==o;
    }

    public List<View> getContentView() {
        return contentView;
    }

}
